/**
  **************************************************************************
  * @file     at32f403a_407_board.c
  * @version  v2.0.7
  * @date     2022-02-11
  * @brief    set of firmware functions to manage leds and push-button.
  *           initialize delay function.
  **************************************************************************
  *                       Copyright notice & Disclaimer
  *
  * The software Board Support Package (BSP) that is made available to 
  * download from Artery official website is the copyrighted work of Artery. 
  * Artery authorizes customers to use, copy, and distribute the BSP 
  * software and its related documentation for the purpose of design and 
  * development in conjunction with Artery microcontrollers. Use of the 
  * software is governed by this copyright notice and the following disclaimer.
  *
  * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  *
  **************************************************************************
  */
#include "at32f403a_407_board.h"
#include "los_interrupt.h"
#include "at32f403a_407_tmr.h"
#include "mySysCfg.h"

#include  "compatible.h"
#include  "syscfg.h"
#include  "mytype.h"
#include  "bsp.h"


/** @addtogroup AT32F403A_407_board
  * @{
  */

/** @defgroup BOARD
  * @brief onboard periph driver
  * @{
  */

/* delay macros */
#define STEP_DELAY_MS                    50

/* delay variable */
static __IO uint32_t fac_us;
static __IO uint32_t fac_ms;

/* support printf function, usemicrolib is unnecessary */
#if (__ARMCC_VERSION > 6000000)
  __asm (".global __use_no_semihosting\n\t");
  void _sys_exit(int x)
  {
    x = x;
  }
  /* __use_no_semihosting was requested, but _ttywrch was */
  void _ttywrch(int ch)
  {
    ch = ch;
  }
  FILE __stdout;
#else
   #ifdef __CC_ARM
  #pragma import(__use_no_semihosting)
  struct __FILE
  {
    int handle;
  };
  FILE __stdout;
  void _sys_exit(int x)
  {
    x = x;
  }
  /* __use_no_semihosting was requested, but _ttywrch was */
  void _ttywrch(int ch)
  {
    ch = ch;
  }
 #endif
#endif

#if defined (__GNUC__) && defined (__clang__)
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif

/**
  * @brief  retargets the c library printf function to the usart.
  * @param  none
  * @retval none
  */
PUTCHAR_PROTOTYPE
{
#if (SYSPRINT_ONOFF == 1)
  #if  ((SYSAPP_ONOFF == 0) || (SYSPRINT_FORCE == 1))
    // app未开放或者采用强力打印时，系统打印采用阻塞型的高波特率打印
    while(usart_flag_get(PRINT_UART, USART_TDBE_FLAG) == RESET);
    usart_data_transmit(PRINT_UART, ch);
  #else
    // app开放+非强力打印时
    if (g_UartInitFlag.BitsField.LocalCom1 == 0)
    {// 未初始化时用阻塞型
        while(usart_flag_get(PRINT_UART, USART_TDBE_FLAG) == RESET);
        usart_data_transmit(PRINT_UART, ch); 
    }
    else
    {// 初始化完成后用app的api
        extern void LocalCom1_SendAppData(u8 *AppTxBuf, u16 AppTxLen);
        LocalCom1_SendAppData((u8 *)&ch, 1);
    }
  #endif
#endif
  return ch;
}

/**
  * @brief  initialize uart
  * @param  baudrate: uart baudrate
            parity: 012分别代表无奇偶
  * @retval none
  */

 
void uart_print_init(uint32_t baudrate, uint32_t parity)
{
  gpio_init_type gpio_init_struct;
  usart_data_bit_num_type WordLength;
  usart_parity_selection_type ATParity;

  /* enable the uart and gpio clock */
  crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, AT_TRUE);
  crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, AT_TRUE);
  crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, AT_TRUE);
  crm_periph_clock_enable(CRM_GPIOD_PERIPH_CLOCK, AT_TRUE);
  crm_periph_clock_enable(CRM_GPIOE_PERIPH_CLOCK, AT_TRUE);
  crm_periph_clock_enable(PRINT_UART_CRM_CLK, AT_TRUE);
  crm_periph_clock_enable(PRINT_UART_TX_GPIO_CRM_CLK, AT_TRUE);
  crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, AT_TRUE);

  gpio_default_para_init(&gpio_init_struct);

  /* configure the uart tx pin */
  gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  gpio_init_struct.gpio_out_type  = GPIO_OUTPUT_PUSH_PULL;
  gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
  gpio_init_struct.gpio_pins = PRINT_UART_TX_PIN;
  gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  gpio_init(PRINT_UART_TX_GPIO, &gpio_init_struct);

    // mux处理
  gpio_pin_remap_config(USART3_GMUX_0001, (confirm_state)1);

    /* configure uart param */
    if (parity == 0)						
    {
        ATParity = USART_PARITY_NONE;
        WordLength = USART_DATA_8BITS;
    }
    else if (parity == 1)
    {
        ATParity = USART_PARITY_ODD;
        WordLength = USART_DATA_9BITS;
    }
    else
    {
        ATParity = USART_PARITY_EVEN;
        WordLength = USART_DATA_9BITS;
    }
  usart_init(PRINT_UART, baudrate, WordLength, USART_STOP_1_BIT);
	usart_parity_selection_config(PRINT_UART, ATParity);						// 校验位设置
  usart_transmitter_enable(PRINT_UART, AT_TRUE);
  usart_enable(PRINT_UART, AT_TRUE);


}


/**
  * @brief  board initialize interface init led and button
  * @param  none
  * @retval none
  */
void at32_board_init()
{
  at32_led_init_1();
  at32_WTD_Init();
}

// led驱动
void at32_led_init_1(void)
{
	gpio_init_type gpio_init_struct;

	crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
	crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
	crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
	crm_periph_clock_enable(CRM_GPIOD_PERIPH_CLOCK, TRUE);
	crm_periph_clock_enable(CRM_GPIOE_PERIPH_CLOCK, TRUE);

	gpio_init_struct.gpio_pins = BD_GPIO_RUNLED_PIN;
	gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
	gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;				// 输出
	gpio_init_struct.gpio_out_type	= GPIO_OUTPUT_PUSH_PULL;
	gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
	gpio_init(BD_GPIO_RUNLED_PORT, &gpio_init_struct);
	BD_GPIO_RUNLED_ON;
}
void at32_Runled_toggle(void)
{
	BD_GPIO_RUNLED_XOR;
}

void at32_WTD_Init(void)
{
	gpio_init_type gpio_init_struct;
	
	// 片外看门狗
	gpio_init_struct.gpio_pins = BD_GPIO_WTDCLR_PIN;
	gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
	gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;				// 输出
	gpio_init_struct.gpio_out_type	= GPIO_OUTPUT_PUSH_PULL;
	gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE;
	gpio_init(BD_GPIO_WTDCLR_PORT, &gpio_init_struct);
}

void at32_WTD_Update(void)
{
	BD_GPIO_WTDCLR_XOR;
}

